package com.jelly.config.quartz;

import com.jelly.common.constant.Constant;
import com.jelly.web.modules.system.quartz.domain.Quartz;
import com.jelly.web.modules.system.quartz.service.IQuartzService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-09-26 18:04
 * @项目名称 jelly
 * @功能描述: 调度工厂类
 **/

@Service
@Component
public class MySchedulerFactory {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    // 任务配置读取服务
    @Autowired
    private IQuartzService quartzService;

    public void scheduleJobs() throws SchedulerException {
        Scheduler scheduler = getScheduler();
        startJob(scheduler);
    }

    // 获取scheduler
    private Scheduler getScheduler(){
        return schedulerFactoryBean.getScheduler();
    }

    // 项目启动 开启任务
    private void startJob(Scheduler scheduler)  {
        try{
            List<Quartz> list = quartzService.getQuartzAll();

            for (Quartz quartz:list){

                // Y-暂停的任务 N-正常运行任务
                if(Constant.ERROR.equals(quartz.getIsState())){

                    continue;

                }
                @SuppressWarnings("unchecked")
                Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(quartz.getJobClassName());
                JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(quartz.getName(), quartz.getType().toString()).build();;
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCron());
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(quartz.getName(), quartz.getType().toString()).withSchedule(scheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);

            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }

    // 任务暂停
    public boolean pauseJob(Quartz quartz) throws SchedulerException {
        Scheduler scheduler = getScheduler();
        Quartz quartz_config = quartzService.updatQuartz(quartz);
        if(null == quartz_config){
            return false;
        }
        JobKey jobKey = JobKey.jobKey(quartz_config.getName(), quartz_config.getType().toString());
        return scheduler.deleteJob(jobKey);

    }


    // 任务恢复
    public boolean resumeJob(Quartz quartz) throws SchedulerException, ClassNotFoundException {
        Scheduler scheduler = getScheduler();
        Quartz quartz_config = quartzService.updatQuartz(quartz);
        if(null == quartz_config){
            return false;
        }
        JobKey jobKey = JobKey.jobKey(quartz_config.getName(), quartz_config.getType().toString());
        Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(quartz_config.getJobClassName());
        JobDetail jobDetail1 = scheduler.getJobDetail(jobKey);
        if (jobDetail1==null){
            JobDetail jobDetail = JobBuilder.newJob(clazz)
                    .withIdentity(quartz_config.getName(), quartz_config.getType().toString()).build();
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartz_config.getCron());
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(quartz_config.getName(), quartz_config.getType().toString())
                    .withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, cronTrigger);
        }else {
            scheduler.resumeJob(jobKey);
        }
        return true;

    }

}
